package fina2.dcs.fi.validator;

import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("licTypeValidator")
@Scope(ScopeType.SESSION)
public class LicenceTypeValidator implements Serializable{

	private ResourceBundle bundle = null;

	public LicenceTypeValidator() {
		UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
		Locale l = root.getLocale();
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		bundle = ResourceBundle.getBundle("messages", l, cl);
	}

	public void validateDescription(FacesContext fc, UIComponent ui, Object value) throws ValidatorException {
		String val = value.toString();
		if (val.trim().length() < 1 || val.trim().length() > 255) {
			FacesMessage fm = new FacesMessage();
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.web.illegal.description.length"));
			throw new ValidatorException(fm);
		}
	}

}
